<?php
require_once("../../class2.php");
require_once(HEADERF);
require_once("./includes/require_all.php");
ramka("open");

$ganrers= array('Action','Adventure','Fighting','FPS','Fun','Hentai','Logical','Manager','MMORPG','Psycho','Quest','Racing','RPG','Simulator','Strategy','Sport','Tactical');
$toappg= $_GET['gname'];

?>
    <link rel="stylesheet" type="text/css" href="txtedit/jquery.cleditor.css" />
    <script type="text/javascript" src="txtedit/jquery.min.js"></script>
    <script type="text/javascript" src="txtedit/jquery.cleditor.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#cgINFO").cleditor();
      });
    </script>
<?php
$userer=USERNAME;
if ($userer!='USERNAME') {

	if (isset($_POST['cgSUBMIT']) OR isset($_POST['cgAPPROUVE'])){
		$cgNAME=mysql_real_escape_string($_POST['cgNAME']);
		$cgCOVER=mysql_real_escape_string($_POST['cgCOVER']);
		$cgYEAR=mysql_real_escape_string($_POST['cgYEAR']);
		$cgDEV=mysql_real_escape_string($_POST['cgDEV']);
		$cgINFO=mysql_real_escape_string($_POST['cgINFO']);
		$cgOS=mysql_real_escape_string($_POST['cgOS']);
		$cgPROG=mysql_real_escape_string($_POST['cgPROG']);
		$cgRAM=mysql_real_escape_string($_POST['cgRAM']);
		$cgVGA=mysql_real_escape_string($_POST['cgVGA']);
		$cgHDS=mysql_real_escape_string($_POST['cgHDS']);
		$cgYOUTUBE=mysql_real_escape_string($_POST['cgYOUTUBE']);
		
		$gc_query = mysql_fetch_array(mysql_query("select max(Prefix) as max_pref from gcs_main"));
		$gc_prefix = intval($gc_query['max_pref']) + 1;
		
		foreach ($_POST['cgganre'] as $cggn) {
			$cgGANRE.=$cggn."/";
		} $cgGANRE=mysql_real_escape_string(substr($cgGANRE,0,-1));
		foreach ($_POST['cgcat'] as $cgct) {
			$cgCAT.=$cgct."/";
		} $cgCAT=mysql_real_escape_string(substr($cgCAT,0,-1));
		if (isset($_POST['cgSUBMIT'])) {
			mysql_query("INSERT INTO gcs_waiting (Game,Prefix,Ganre,Year,AddedBy,Cover,Developper,Platform,YouTube,Info,MinOS,MinPROC,MinRAM,MinVGA,MinHDS) VALUES
			('$cgNAME','$gc_prefix','$cgGANRE','$cgYEAR','".USERNAME."','$cgCOVER','$cgDEV','$cgCAT','$cgYOUTUBE','$cgINFO','$cgOS','$cgPROG','$cgRAM','$cgVGA','$cgHDS')");
		}
		if (isset($_POST['cgAPPROUVE'])&&in_array(USERNAME,$mods,true)) {
				$fnam = $gc_prefix; $exto=explode('.',$cgCOVER); $extend=end($exto);
				$sourcecode = file_get_contents($cgCOVER);
				file_put_contents("gts/covers/$fnam.$extend", $sourcecode);
				$cgCOVER="$fnam.$extend";
			$AP= mysql_fetch_array(mysql_query("SELECT * FROM gcs_waiting WHERE Prefix='$toappg'"));
			mysql_query("INSERT INTO gcs_main (Game,Ganre,Year,AddedBy,Cover,Developper,Platform,YouTube,Info,MinOS,MinPROC,MinRAM,MinVGA,MinHDS,AddedOn,LastChange) VALUES ('$cgNAME','$cgGANRE','$cgYEAR','$AP[AddedBy]','$cgCOVER','$cgDEV','$cgCAT','$cgYOUTUBE','$cgINFO','$cgOS','$cgPROG','$cgRAM','$cgVGA','$cgHDS','$AP[AddedOn]',now())");
			unset ($AP);
			mysql_query("ALTER TABLE gcs_mygames ADD g$toappg DECIMAL NOT NULL DEFAULT '0'");
			mysql_query("DELETE FROM gcs_waiting WHERE Prefix='$toappg'");
		}
		echo "<center class='forumheader3 textRED'>Играта беше въведена в каталога.</center>";
	}

	if (isset($_GET['gname'])&&in_array(USERNAME,$mods,true)) {
		$AP= mysql_fetch_array(mysql_query("SELECT * FROM gcs_waiting WHERE Prefix='$toappg'"));
		$plts= explode("/",$AP['Platform']);
		foreach ($plts as $val) {
			$chkdg[$val]="checked='yes'";
		}
		$grs= explode("/",$AP['Ganre']);
		foreach ($grs as $val) {
			$chkdg[$val]="checked='yes'";
		}
		$addbuttn['name']= 'cgAPPROUVE';
		$addbuttn['value']= 'Одобри';
		$ifadd="?gname=".$_GET['gname'];
	}
	else {
		$AP['Game']='Име на играта';
		$AP['Year']='(формат: 2012)';
		$AP['Info']='Инфо за играта';
		$AP['YouTube']='(формат: http://www.youtube.com/watch?v=xxxxxxxxxxx)';
		$divdisp="style='display: none;'";
		$addbuttn['name']= 'cgSUBMIT';
		$addbuttn['value']= 'Изпрати';
	}
	
	$classed= "class='tbox'";
	echo "
		<form name='input' action='gts-add.php$ifadd' method='post'>
		<div class='forumheader3'>
		<h1 class='forumheader3 textWHITE'><center><INPUT type='text' name='cgNAME' value=\"$AP[Game]\" $classed></INPUT></center></h1>
		<table width=100% class='forumheader3'>
			<tr>
			<td class='forumheader3' width=30%><center><img src='$AP[Cover]' height=250px style='vertical-align:middle; margin-top:3px;'/><INPUT type='text' name='cgCOVER' $classed value='$AP[Cover]'><br>URL на картинката</INPUT></center><br></td>
			<td style='vertical-align:top;'>
				<table width=100%>
					<tr>
					<td class='forumheader3' width=30%> Година</td>
					<td class='forumheader'><INPUT type='text' name='cgYEAR' value='$AP[Year]' $classed></INPUT></td>
					</tr>
					<tr>
					<td class='forumheader3' width=30%> Разработчик</td>
					<td class='forumheader'><INPUT type='text' name='cgDEV' $classed value='$AP[Developper]'></INPUT></td>
					</tr>
					<tr>
					<td class='forumheader3' width=30%> Платформа</td>
					<td class='forumheader'>
						<INPUT type='checkbox' name='cgcat[]' value='PC' onclick=toggle('minsys') $chkdg[PC]>PC</INPUT>
						<INPUT type='checkbox' name='cgcat[]' value='PS2' $chkdg[PS2]>PS2</INPUT>
						<INPUT type='checkbox' name='cgcat[]' value='PS3' $chkdg[PS3]>PS3</INPUT>
						<INPUT type='checkbox' name='cgcat[]' value='XBOX' $chkdg[XBOX]>XBOX</INPUT>
						<INPUT type='checkbox' name='cgcat[]' value='XBOX360' $chkdg[XBOX360]>XBOX360</INPUT>
					</td>
					</tr>				
					<tr>
					<td class='forumheader3' width=30%> Жанр</td>
					<td class='forumheader'>";

	foreach ($ganrers as $ganro) {
		echo "<INPUT type='checkbox' name='cgganre[]' value='$ganro' $chkdg[$ganro]>$ganro</INPUT>";
		if ($ganro=='Quest' or $ganro=='Sport') echo '<br>';
	}
	echo "</td>
			</tr>
			<tr>
			<td class='forumheader3' width=30%> YouTube Трейлър</td>
			<td class='forumheader'>
				<INPUT type='text' name='cgYOUTUBE' value='$AP[YouTube]' $classed size=60></INPUT>
			</td>
			</tr>
		</table>
	</td>
	</tr>
	</table>
	</div>
	<table width=100% class='forumheader3'>
	<tr>
	<td width=70%>
		<div class='forumheader3'> За играта</div>
		<div class='forumheader textGREY'><TEXTAREA name='cgINFO' id='cgINFO' $classed style='width:99%; min-height:180px;'>$AP[Info]</TEXTAREA></div>
		<div class='forumheader3'></div>
	</td>
	<td class='forumheader3' style='vertical-align:top;'>
		<div id='minsys' $divdisp>
		<div class='forumheader3'><center>Минимални изисквания</center></div>
		<center class='forumheader' style='font-weight:normal'>
			Операционна система:<INPUT type='text' name='cgOS' $classed value='$AP[MinOS]'></INPUT><br>
			Процесор:<br><INPUT type='text' name='cgPROG' $classed value='$AP[MinProc]'></INPUT><br>
			РАМ памет:<br><INPUT type='text' name='cgRAM' $classed value='$AP[MinRAM]'></INPUT><br>
			Видео карта:<br><INPUT type='text' name='cgVGA' $classed value='$AP[MinVGA]'></INPUT><br>
			Твърд диск:<br><INPUT type='text' name='cgHDS' $classed value='$AP[MinHDS]'></INPUT><br>
		</center>
		</div>
	</td>
	</tr>
	</table>
	<center class='forumheader3'><input type='submit' name='$addbuttn[name]' value='$addbuttn[value]' class='button'/></center>
	</form>
	";
}
else echo "<div class='forumheader3 textRED'>Само регистрираните потребители могат да добавят игри в каталога.</div>";

ramka("close");
require_once(FOOTERF);
?>